package com.tongji.housediy.view.titlewindows
{
	import com.adobe.cairngorm.popup.PopUpEvent;
	import com.tongji.housediy.events.TransferPicModelEvent;
	import com.tongji.housediy.events.commonEvent.ColorPreviewAdminEvent;
	import com.tongji.housediy.events.commonEvent.PopupBtnAdminEvent;
	import com.tongji.housediy.events.commonEvent.SetCompStateEvent;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.service.GoodsService;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.view.components.RoomBorder;
	import com.tongji.housediy.view.components.RoomFloor;
	import com.tongji.housediy.view.components.RoomShape;
	import com.tongji.housediy.vo.Color;
	import com.tongji.housediy.vo.Goods;
	import com.tongji.housediy.vo.Model;
	import com.tongji.housediy.vo.PicModel;
	import com.tongji.housediy.vo.Size;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.PopUpButton;
	import mx.events.ColorPickerEvent;
	import mx.events.IndexChangedEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class MaterialColorViewMediator extends Mediator
	{
		[Inject]
		public var view:MaterialColorAdminView;
		
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		[Inject]
		public var service:GoodsService;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.goodsName, "text", model, "goodsName");
			BindingUtils.bindProperty(view.brandName, "text", model, "goodsBrand");
			BindingUtils.bindProperty(view.goodsSizeCmb, "dataProvider", model, "models");
			BindingUtils.bindProperty(view.goodsType, "text", model, "goodsType");
			BindingUtils.bindProperty(view.colorList, "dataProvider", model, "colorArray");
			BindingUtils.bindProperty(view.titleLabel, "text", model, "titleName");
			mediatorMap.createMediator(view.popupBtn.popUp);
			if (model.models)
			{
				for (var i:int = 0; i < model.models.length; i++)
				{
					if (model.titleName == "踢脚线色彩")
					{
						if ((model.models.getItemAt(i) as Model).modelID == (selectedModel.selectedObj as RoomBorder).skirtingSelectedSizeID)
						{
							view.goodsSizeCmb.selectedIndex = i;
							break;
						}
					}
					else if ((model.models.getItemAt(i) as Model).modelID == (selectedModel.selectedObj as RoomShape).selectedSizeID)
					{
						view.goodsSizeCmb.selectedIndex = i;
						break;
					}
				}
			}
			
			if (model.colorArray)
			{
				for (i = 0; i < model.colorArray.length; i++)
				{
					if (model.titleName == "踢脚线色彩")
					{
						if ((model.colorArray.getItemAt(i) as Color).colorID == (selectedModel.selectedObj as RoomBorder).skirtingSelectedColorID)
						{
							view.colorList.selectedIndex = i;
							break;
						}
					}
					else if ((model.colorArray.getItemAt(i) as Color).colorID == (selectedModel.selectedObj as RoomShape).selectedColorID)
					{
						view.colorList.selectedIndex = i;
						break;
					}
				}
			}
			
			var width:Number;
			var height:Number;
			switch (model.titleName)
			{
				case "地面色彩":
					view.image.source = new Bitmap((selectedModel.selectedObj as RoomFloor).materialBitmapData);
					view.image.width = (selectedModel.selectedObj as RoomFloor).materialBitmapData.width;
					view.image.height = (selectedModel.selectedObj as RoomFloor).materialBitmapData.height;
					
					view.tempFloorGoods = (selectedModel.selectedObj as RoomFloor).hostGoods;
					view.tempFloorMap = (selectedModel.selectedObj as RoomFloor).materialBitmapData;
					
					this.addContextListener(SetCompStateEvent.RESET_FLOOR_LIST_INDEX, resetListIndexHandler, SetCompStateEvent);
					break;
				case "墙面色彩":
					view.image.source = new Bitmap((selectedModel.selectedObj as RoomBorder).wallMap);
					width = (selectedModel.selectedObj as RoomBorder).wallMap.width;
					height = (selectedModel.selectedObj as RoomBorder).wallMap.height;
					view.image.width = ((width == 2) ? 64 : width);
					view.image.height = ((height == 2) ? 64 : height);
					
					view.tempWallGoods = (selectedModel.selectedObj as RoomBorder).hostGoods;
					view.tempWallMap = (selectedModel.selectedObj as RoomBorder).wallMap;
					
					this.addContextListener(SetCompStateEvent.RESET_WALL_LIST_INDEX, resetListIndexHandler, SetCompStateEvent);
					break;
				case "踢脚线色彩":
					view.image.source = new Bitmap((selectedModel.selectedObj as RoomBorder).skirtingMap);
					width = (selectedModel.selectedObj as RoomBorder).skirtingMap.width;
					height = (selectedModel.selectedObj as RoomBorder).skirtingMap.height;
					view.image.width = ((width == 2) ? 64 : width);
					view.image.height = ((height == 2) ? 64 : height);
					
					view.tempSkirtingGoods = (selectedModel.selectedObj as RoomBorder).skirtingHostGoods;
					view.tempSkirtingMap = (selectedModel.selectedObj as RoomBorder).skirtingMap;
					
					this.addContextListener(SetCompStateEvent.RESET_SKIRTING_LIST_INDEX, resetListIndexHandler, SetCompStateEvent);
					break;
			}
			
			/*if (selectedModel.selectedObj is RoomFloor)
			{
				view.image.source = new Bitmap((selectedModel.selectedObj as RoomFloor).materialBitmapData);
				view.image.width = (selectedModel.selectedObj as RoomFloor).materialBitmapData.width;
				view.image.height = (selectedModel.selectedObj as RoomFloor).materialBitmapData.height;
			}
			else if (selectedModel.selectedObj is RoomBorder)
			{
				view.image.source = new Bitmap((selectedModel.selectedObj as RoomBorder).wallMap);
				var width:Number = (selectedModel.selectedObj as RoomBorder).wallMap.width;
				var height:Number = (selectedModel.selectedObj as RoomBorder).wallMap.height;
				view.image.width = ((width == 2) ? 64 : width);
				view.image.height = ((height == 2) ? 64 : height);
			}*/
			
			view.goodsSizeCmb.addEventListener(IndexChangeEvent.CHANGE, changeSizeColorHandler);
			view.colorList.addEventListener(IndexChangeEvent.CHANGE, changeSizeColorHandler);
//			view.colorPicker.addEventListener(ColorPickerEvent.CHANGE, colorPickerChangeHandler);
			
			this.addContextListener(PopupBtnAdminEvent.CLOSE_POPUP_BUTTON, closePopupBtnHandler, PopupBtnAdminEvent);
			this.addContextListener(TransferPicModelEvent.TRANSFER_PIC_MODEL, transferPicModelHandler, TransferPicModelEvent);
			
			/*this.addContextListener(SetCompStateEvent.RESET_WALL_LIST_INDEX, resetListIndexHandler, SetCompStateEvent);
			this.addContextListener(SetCompStateEvent.RESET_SKIRTING_LIST_INDEX, resetListIndexHandler, SetCompStateEvent);
			this.addContextListener(SetCompStateEvent.RESET_FLOOR_LIST_INDEX, resetListIndexHandler, SetCompStateEvent);*/
		}
		
		private function changeSizeColorHandler(event:IndexChangeEvent):void
		{
			service.getPicModel((view.goodsSizeCmb.selectedItem as Size).sizeID, (view.colorList.selectedItem as Color).colorID);
		}
		
		private function transferPicModelHandler(event:TransferPicModelEvent):void
		{
			switch (model.titleName)
			{
				case "地面色彩":
					view.tempFloorPicModel = event.picModel;
					break;
				case "墙面色彩":
					view.tempWallPicModel = event.picModel;
					break;
				case "踢脚线色彩":
					view.tempSkirtingPicModel = event.picModel;
					break;
			}
			
			var loader:Loader = LoadAdmin.loadBitmapData(event.picModel.dragPicSrc);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
		}
		
		private function loadCompleteHandler(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE, loadCompleteHandler);
			
			switch (model.titleName)
			{
				case "地面色彩":
					view.tempFloorMap = (event.target.content as Bitmap).bitmapData;
					break;
				case "墙面色彩":
					view.tempWallMap = (event.target.content as Bitmap).bitmapData;
					break;
				case "踢脚线色彩":
					view.tempSkirtingMap = (event.target.content as Bitmap).bitmapData;
					break;
			}
			
			view.image.source = event.target.content;
			view.image.width = (event.target.content as Bitmap).bitmapData.width;
			view.image.height = (event.target.content as Bitmap).bitmapData.height;
			
			view.dispatchEvent(new ColorPreviewAdminEvent(ColorPreviewAdminEvent.CHANGE_IMAGE, event.target.content.bitmapData));
		}
		
		private function closePopupBtnHandler(event:PopupBtnAdminEvent):void
		{
			view.popupBtn.close();
			
			var tempDragSrc:String;
			switch (model.titleName)
			{
				case "地面色彩":
					view.tempFloorGoods = event.hostGoods;
					view.tempFloorPicModel = new PicModel();
					view.tempFloorPicModel.selectedColorID = view.tempFloorGoods.selectedColorID;
					view.tempFloorPicModel.selectedSizeID = view.tempFloorGoods.selectedSizeID;
					tempDragSrc = view.tempFloorGoods.dragPicSrc;
					resetSizeColorIndex(view.tempFloorGoods);
					break;
				case "墙面色彩":
					view.tempWallGoods = event.hostGoods;
					view.tempWallPicModel = new PicModel();
					view.tempWallPicModel.selectedColorID = view.tempWallGoods.selectedColorID;
					view.tempWallPicModel.selectedSizeID = view.tempWallGoods.selectedSizeID;
					tempDragSrc = view.tempWallGoods.dragPicSrc;
					resetSizeColorIndex(view.tempWallGoods);
					break;
				case "踢脚线色彩":
					view.tempSkirtingGoods = event.hostGoods;
					view.tempSkirtingPicModel = new PicModel();
					view.tempSkirtingPicModel.selectedColorID = view.tempSkirtingGoods.selectedColorID;
					view.tempSkirtingPicModel.selectedSizeID = view.tempSkirtingGoods.selectedSizeID;
					tempDragSrc = view.tempSkirtingGoods.dragPicSrc;
					resetSizeColorIndex(view.tempSkirtingGoods);
					break;
			}
			
			var loader:Loader = LoadAdmin.loadBitmapData(tempDragSrc);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
			
			/*view.tempFloorGoods = event.hostGoods;
			
			view.tempFloorPicModel = new PicModel();
			view.tempFloorPicModel.selectedColorID = view.tempFloorGoods.selectedColorID;
			view.tempFloorPicModel.selectedSizeID = view.tempFloorGoods.selectedSizeID;*/
			
			/*for (var i:int = 0; i < model.sizes.length; i++)
			{
				if ((model.sizes.getItemAt(i) as Size).sizeID == view.tempFloorGoods.selectedSizeID)
				{
					view.goodsSizeCmb.selectedIndex = i;
					break;
				}
			}
			
			for (i = 0; i < model.colorArray.length; i++)
			{
				if ((model.colorArray.getItemAt(i) as Color).colorID == view.tempFloorGoods.selectedColorID)
				{
					view.colorList.selectedIndex = i;
					break;
				}
			}
			
			var loader:Loader = LoadAdmin.loadBitmapData(view.tempFloorGoods.dragPicSrc);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);*/
		}
		
		private function resetSizeColorIndex(goods:Goods):void
		{
			if (model.models)
			{
				for (var i:int = 0; i < model.models.length; i++)
				{
					if ((model.models.getItemAt(i) as Model).modelID == goods.selectedSizeID)
					{
						view.goodsSizeCmb.selectedIndex = i;
						break;
					}
				}
			}
			
			if (model.colorArray)
			{
				for (i = 0; i < model.colorArray.length; i++)
				{
					if ((model.colorArray.getItemAt(i) as Color).colorID == goods.selectedColorID)
					{
						view.colorList.selectedIndex = i;
						break;
					}
				}
			}
		}
		
		private function resetListIndexHandler(event:SetCompStateEvent):void
		{
			switch (model.titleName)
			{
				case "地面色彩":
					resetSizeColorIndex(view.tempFloorGoods);
					break;
				case "墙面色彩":
					resetSizeColorIndex(view.tempWallGoods);
					break;
				case "踢脚线色彩":
					resetSizeColorIndex(view.tempSkirtingGoods);
					break;
			}
		}
		
		private function colorPickerChangeHandler(event:ColorPickerEvent):void
		{
			var bitmapData:BitmapData = new BitmapData(2, 2, false, event.target.selectedColor);
			
			switch (model.titleName)
			{
				case "地面色彩":
					view.tempFloorGoods = null;
					view.tempFloorPicModel = null;
					view.tempFloorMap = bitmapData;
					break;
				case "墙面色彩":
					view.tempWallGoods = null;
					view.tempWallPicModel = null;
					view.tempWallMap = bitmapData;
					break;
				case "踢脚线色彩":
					view.tempSkirtingGoods = null;
					view.tempSkirtingPicModel = null;
					view.tempSkirtingMap = bitmapData;
					break;
			}
			
			model.arrangeSizes(null);
			view.image.source = new Bitmap(bitmapData);
			view.image.width = 64;
			view.image.height = 64;
			
			view.dispatchEvent(new ColorPreviewAdminEvent(ColorPreviewAdminEvent.CHANGE_IMAGE, view.image.source.bitmapData));
		}
	}
}